package com.igeek.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.car.constant.MessageConstant;
import com.igeek.car.entity.XianLu;
import com.igeek.car.result.Result;
import com.igeek.car.service.XianLuService;
import com.igeek.car.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * XianLuController：线路接口。仅仅只做接口暴露
 * XianLuService 线路业务接口
 *     XianLuServiceImpl线路具体业务实现
 *   XianLuMapper线路接口实现的类
 * Swagger测试地址：http://localhost:8080/swagger-ui.html
 *
 * @author bbjs
 * @since 2024/12/4
 */
@RestController
@RequestMapping("/admin/xianlu")
@Api(tags = "线路的接口管理")
public class XianLuController {

    @Autowired
    private XianLuService xianLuService;

    @ApiOperation("分页条件查询线路")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页参数") PageQueryVO vo){
        Page<XianLu> page = xianLuService.findPage(vo);
        if(page != null){
            return new Result(true, MessageConstant.QUERY_XIANLU_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_XIANLU_FAIL);
    }

    @ApiOperation("新增线路")
    @PostMapping("add")
    public Result add(@RequestBody XianLu xianLu){
        boolean flag = xianLuService.save(xianLu);
        if(flag){
            return new Result(true, MessageConstant.ADD_XIANLU_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_XIANLU_FAIL);
    }
    @ApiOperation("通过ID查询线路信息")
    @GetMapping("{id}")
    public Result findOne(@PathVariable("id") String id){
        XianLu xianLu = xianLuService.getById(id);
        if(xianLu != null){
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, xianLu);
        }
        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }
    @ApiOperation("编辑线路")
    @PutMapping("update")
    public Result update(@RequestBody XianLu xianLu){
        boolean flag = xianLuService.updateById(xianLu);
        if(flag){
            return new Result(true, MessageConstant.UPDATE_XIANLU_SUCCESS);
        }
        return new Result(false, MessageConstant.UPDATE_XIANLU_FAIL);
    }

    @ApiOperation("删除线路")
    @DeleteMapping("{routeId}")
    public Result delete(@PathVariable("routeId") String routeId) {
        try {
            // 调用服务层删除线路
            boolean flag = xianLuService.del(routeId);
            if (flag) {
                return new Result(true, MessageConstant.DELETE_XIANLU_SUCCESS);
            }
            return new Result(false, MessageConstant.DELETE_XIANLU_FAIL);
        } catch (RuntimeException e) {
            // 捕获删除时的异常并返回自定义的错误信息
            return new Result(false, e.getMessage());  // 返回异常信息
        }
    }

}